package com.guit.hrms.mapper;

import com.guit.hrms.dto.interview.InterviewDTO;
import com.guit.hrms.po.interview.Interview;
import com.guit.hrms.po.interview.InterviewSituation;
import com.guit.hrms.po.interview.Interviewee;
import com.guit.hrms.po.interview.Offer;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface InterviewMapper {

    /**
     * 添加面试安排
     * @param interview 面试安排信息
     * @return Result
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into interview_scheduling (plan_id, dept_id, position_number, interviewer_ids,interview_round, name, place, recruits_number, start_time, end_time, create_time, update_time, create_by, update_by) values (#{planId}, #{deptId}, #{positionNumber}, #{interviewerIds}, #{interviewRound}, #{name}, #{place}, #{recruitsNumber}, #{startTime}, #{endTime}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy})")
    Integer addInterview(Interview interview);

    /**
     * 根据面试id查询面试安排
     * @param id 面试安排id
     * @return 面试安排
     */
    @Select("select * from interview_scheduling where id=#{id}")
    Interview getInterviewById(Integer id);

    /**
     * 查询面试安排是否重复
     * @param interviewDTO 面试安排信息
     * @return Result
     */
    @Select("select count(*) from interview_scheduling where start_time<#{endTime} and end_time>#{startTime} and place=#{place}")
    Integer selectIsRepeatPlaceAndTime(InterviewDTO interviewDTO);

    /**
     * 查询面试安排是否重复
     * @param interviewDTO 面试安排信息
     * @param interviewId 面试安排id
     * @return Result
     */
    @Select("select count(*) from interview_scheduling where start_time<#{interviewDTO.endTime} and end_time>#{interviewDTO.startTime} and place=#{interviewDTO.place} and id!=#{interviewId}")
    Integer selectIsRepeatPlaceAndTime1(InterviewDTO interviewDTO, Integer interviewId);

    /**
     * 获取个人面试安排
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param employeeId 员工号
     * @return 个人面试安排
     */
//    @Select("select * from interview_scheduling where start_time>=#{startTime} and end_time<=#{endTime} and interviewer_ids like '%'+#{employeeId}+'%'")
    List<Interview> getPersonalInterviews(LocalDateTime startTime, LocalDateTime endTime, String employeeId);

    /**
     * 获取应聘者信息
     * @param id 面试安排id
     * @return 应聘者信息
     */
    @Select("select * from interviewee where id in (select interviewee_id from interview_situation where interview_id=#{id})")
    List<Interviewee> getIntervieweeMsg(Integer id);

    /**
     * 根据id查询应聘者信息
     * @param id 主键id
     * @return 应聘者信息
     */
    @Select("select * from interviewee where id=#{id}")
    Interviewee getIntervieweeById(Integer id);

//    /**
//     * 获取所有面试安排
//     * @param startTime 开始时间
//     * @param endTime 结束时间
//     * @return 所有面试安排
//     */
//    @Select("select * from interview_scheduling where start_time>=#{startTime} and end_time<=#{endTime}")
//    List<Interview> getAllInterviews(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 更新面试安排
     * @param interview 面试安排信息
     * @return Result
     */
    Integer updateInterview(Interview interview);

    /**
     * 获取面试安排详情
     * @param interviewId 面试安排id
     * @param intervieweeId 应聘者id
     * @return 面试安排详情列表
     */
    List<InterviewSituation> getInterviewSituations(Integer interviewId, Integer intervieweeId);


    /**
     * 获取面试安排详情
     * @param id 面试详情id
     * @return 面试安排详情
     */
    @Select("select * from interview_situation where id=#{id}")
    InterviewSituation getInterviewSituation(Integer id);

    /**
     * 删除面试安排
     * @param interviewId 面试安排id
     * @return Result
     */
    @Delete("delete from interview_scheduling where id = #{interviewId}")
    Integer deleteInterviewById(Integer interviewId);

    /**
     * 删除面试安排情况
     * @param interviewId 面试安排id
     * @return Result
     */
    @Delete("delete from interview_situation where interview_id = #{interviewId}")
    Integer deleteInterviewSituationByInterviewId(Integer interviewId);

    /**
     * 更新面试详情（为应聘者评分）
     * @param interviewSituation 面试详情信息
     * @return Result
     */
    Integer updateInterviewSituation(InterviewSituation interviewSituation);

    /**
     * 为应聘者分配面试安排
     * @param interviewSituation 面试安排情况
     * @return Result
     */
    @Insert("insert into interview_situation(interview_id, interviewee_id, create_time, update_time) values (#{interviewId}, #{intervieweeId}, #{createTime}, #{updateTime})")
    Integer assignedInterviewForInterviewee(InterviewSituation interviewSituation);

    /**
     * 修改参加面试人数
     * @param interviewId 面试安排id
     * @return Result
     */
    @Update("update interview_scheduling set interview_number=(interview_number + #{number}) where id=#{interviewId}")
    Integer updateInterviewNumber(Integer interviewId, Integer number);

    /**
     * 修改面试通过人数
     * @param interviewId 面试安排id
     * @param number 人数
     * @return Result
     */
    @Update("update interview_scheduling set through_number=(through_number + #{number}) where id=#{interviewId}")
    Integer updateThroughNumber(Integer interviewId, Integer number);

    /**
     * 删除应聘者的面试详情
     * @param intervieweeId 应聘者id
     * @return Result
     */
    @Delete("delete from interview_situation where interviewee_id=#{intervieweeId}")
    Integer deleteInterviewSituationByIntervieweeId(Integer intervieweeId);

    /**
     * 根据应聘者id查询对应的面试安排
     * @param intervieweeId 应聘者id
     * @return 面试安排列表
     */
    @Select("select * from interview_scheduling where id in (select interview_id from interview_situation where interviewee_id=#{intervieweeId})")
    List<Interview> getInterviewIdByIntervieweeId(Integer intervieweeId);

    /**
     * 取消应聘者的面试安排
     * @param interviewSituation 面试安排情况
     * @return Result
     */
    @Delete("delete from interview_situation where interview_id=#{interviewId} and interviewee_id=#{intervieweeId}")
    Integer cancelIntervieweesInterview(InterviewSituation interviewSituation);

    /**
     * 根据面试id查询应聘者id
     * @param id 面试id
     * @return 结果
     */
    @Select("select interviewee_id from interview_situation where id = #{id}")
    Integer getIntervieweeByInterviewId(Integer id);

    /**
     * 查询所有有效的面试安排
     * @return 面试安排列表
     */
    @Select("select * from interview_scheduling where start_time > now()")
    List<Interview> findAllActiveInterviews();
}
